#!/usr/bin/env python
import sys
import numpy as np
from numpy import exp

t=float(sys.argv[2])
convert = 0.01036427230133138/8.617333262145e-5/t

if __name__ == '__main__':

    k = sys.argv[1]

    alldata = np.load(f"{k}.npz", allow_pickle=True)
    alldata = alldata['alldata']
    weights = np.loadtxt(f"{k}_weights.data")

    allphi = []
    allxi = []
    allbias = []
    for i, idata in enumerate(alldata):
        phi = idata['colvar']
        allphi += [phi]
        xi = idata['xi']
        biasE = 0.5*idata['k']*(xi-idata['x0'])**2*convert+idata['k0']*(xi-idata['x0'])*convert
        allxi += [xi]
        allbias += [biasE]
    allphi = np.vstack(allphi).T
    allxi = np.hstack(allxi)
    allbias = np.hstack(allbias)
    print(np.min(allbias), np.max(allbias))
    print(allphi.shape, allxi.shape, allbias.shape)
    weights = weights/exp(-allbias)
    alldata = np.vstack((allxi, allphi, weights)).T
    print(alldata.shape)

    np.savetxt(f"{k}_weights.txt", alldata)

